﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using Htss.Platform.Data;
using DreamBox.DAL;

namespace DreamBox.BLL.Search
{
    public class search
    {
        //搜索关键字
        private string keywords;

        public string Keywords
        {
            get { return keywords; }
            set { keywords = value; }
        }
        //页码
        private int pid;

        public int Pid
        {
            get { return pid; }
            set { pid = value; }
        }
        //获取关键字
        private string GetWhere()
        {
            StringBuilder str = new StringBuilder();
            str.Append("(Title like '%");
            str.Append(Keywords);
            str.Append("%'");
            str.Append(" or ");
            str.Append(" KeyWords like '%");
            str.Append(Keywords);
            str.Append("%')");

            return str.ToString();
        }
        //获取记录数
        public int GetTotal()
        {
            Formpage page = new Formpage();
            //查询记录总数
            DataTable total = page.Total("grapes_v_topic", "TID", 10, 1, GetWhere());
            return int.Parse(total.Rows[0]["total"].ToString());
        }
        //分页
        public string Page()
        {

            int rownum = GetTotal();
            /*分页Start*************************/

            int pagesize = 10;

            int rowcount = 0;
            //判断总页码
            if (rownum % pagesize == 0)
            {
                rowcount = rownum / pagesize;
            }
            else
            {
                rowcount = rownum / pagesize + 1;
            }
            if (rowcount == 0)
            {
                rowcount = 1;
            }
            StringBuilder str = new StringBuilder();


            //循环生成分页连接
            for (int i = 1; i <= rowcount; i++)
            {
                StringBuilder s = new StringBuilder();
                if (pid > 9)
                {
                    if (i < (pid - 4) && i > 1)
                    {
                        continue;
                    }

                }
                if (i > (pid + 4) && i < rowcount)
                {
                    break;
                }
                //链接替换
                if (pid == i)
                {
                    s.Append(i);
                    s.Append("&nbsp;");
                }
                else
                {
                    s.Append("<a href='/search/list/vlist?page=");
                    s.Append(i);
                    s.Append("&");
                    s.Append("kw=");
                    s.Append(Keywords);
                    s.Append("'");
                    s.Append(">[");
                    s.Append(i);
                    s.Append("]</a>&nbsp;");
                }
                str.Append(s);
            }
            return str.ToString();
        }
        public DataTable PageBind()
        {
            Formpage page = new Formpage();
            DataTable t = page.Where("grapes_v_topic", "TID", 10, Pid, GetWhere());

            //实例化新DataTable
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Title", typeof(string)));
            dt.Columns.Add(new DataColumn("KeyWords", typeof(string)));
            dt.Columns.Add(new DataColumn("Url", typeof(string)));
            dt.Columns.Add(new DataColumn("PostDate", typeof(string)));
            dt.Columns.Add(new DataColumn("TotalCount", typeof(int)));

            foreach (DataRow row in t.Rows)
            {
                //处理地址
                string url = "/topic/read/vread/" + row["TID"].ToString();
                string title = row["Title"].ToString();

                //数据填充
                DataRow NewRow = dt.NewRow();
                NewRow["Title"] = title.Replace(Keywords, "<font color='red'>" + Keywords + "</font>");

                NewRow["KeyWords"] = row["KeyWords"].ToString();
                

                NewRow["Url"] = url;
                NewRow["PostDate"] = DateTime.Parse(row["PostDate"].ToString()).ToShortDateString();

                //处理统计列
                Regex r = new Regex(@"(" + Keywords + ")", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                NewRow["TotalCount"] = r.Matches(title).Count;

                //填充行
                dt.Rows.Add(NewRow);
            }
            dt.DefaultView.Sort = "TotalCount desc";

            return dt;
        }
    }
}
